//
//  KCReimurseController.swift
//  MyKC
//
//  Created by yy on 2017/2/23.
//  Copyright © 2017年 com.yasuo. All rights reserved.
//

import UIKit
import SVProgressHUD
import YYModel

class KCReimurseController: UITableViewController {
    lazy var reimTypeArray = [KCReimTypeModel]()
    override func viewDidLoad() {
        super.viewDidLoad()
        setupUI()
        getData()
    }
}

// MARK: -  设置UI界面
extension KCReimurseController {
    internal func setupUI() {
        tableView.backgroundColor = UIColor(colorLiteralRed: Float(242) / Float(255), green: Float(242) / Float(255), blue: Float(242) / Float(255), alpha: 1)
        tableView.tableFooterView = UIView()
        navigationItem.title = "报销"
        navigationItem.rightBarButtonItem = UIBarButtonItem(title: "申请", style: .plain, target: self, action: #selector(shenqingBtnClick(sender:)))
        //注册cell
        tableView.register(UINib(nibName: "KCReimburseContentCell", bundle: nil), forCellReuseIdentifier: "KCReimburseContentCellId")
        tableView.register(UINib(nibName: "KCReimburseDetailCell", bundle: nil), forCellReuseIdentifier: "KCReimburseDetailCellId")
    }
    
    func shenqingBtnClick(sender: UIBarButtonItem) {
        
    }
}

// MARK: -  处理网络数据
extension KCReimurseController {
    func getData() {
        SVProgressHUD.show()
       KCNetworkManager.sharedTools.request(method: .GET, urlString: "/api/Personal/GetReimbursementType", parameters: nil) { [weak self] (response, error) in
        guard let responseObj = response as? NSDictionary else {
            SVProgressHUD.dismiss()
            return
        }
        SVProgressHUD.dismiss()
        guard let responseArray = responseObj["Content"] as? NSArray else {
            return
        }
        self?.reimTypeArray = NSArray.yy_modelArray(with: KCReimTypeModel.self, json: responseArray) as? [KCReimTypeModel] ?? []
        self?.tableView.reloadData()
        }
    }
}

// MARK: -  tableView的代理方法------------
extension KCReimurseController {
    // MARK: - Table view data source
    override func numberOfSections(in tableView: UITableView) -> Int {
        return 2
    }
    
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 1
    }
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: getId(indexPath: indexPath), for: indexPath)
        if indexPath.section == 0 {
            let cell = cell as! KCReimburseContentCell
            //点击的时候给cell赋值  此时数组里面肯定是有值的
            cell.typeArray = self.reimTypeArray
//            cell.reimburseBlock = { [weak self] in
//                cell.typeArray = self?.reimTypeArray
//            }
        }
        //设置cell的显示
        cell.selectionStyle = .none
        return cell
    }
    
    override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return indexPath.section == 0 ? 45 : 235
    }
    
    override func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return 10
    }
    
    func getId(indexPath: IndexPath) -> String {
        return indexPath.section == 0 ? "KCReimburseContentCellId" : "KCReimburseDetailCellId"
    }
}

